﻿/*
 *  PassBag is multi-platform software that securely manage/store passwords
 *  Copyright (C) <2009>  <Hugo Rodrigues PEREIRA>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *	
 *  You can find a copy of the GPLv3 at project <root directory>/gpl.txt
 *
 */

using System.Collections.Generic;

namespace PassBag.Utils {
    /// <summary>
    /// Hekper class to synchronize two list
    /// </summary>
    /// <remarks>
    ///   Usage
    ///   =====
    ///   List<string> sourceList = new List<string> { "Black", "Blue", "Green"};
    ///   List<string> destinationList = new List<string> { "Black", "Blue", "Yellow" };
    ///
    ///   ListSynchronizer.SynchronizeSortedList (sourceList, destinationList, StringComparer.CurrentCultureIgnoreCase,
    ///                                           onlyOnSourceList, onlyOnDestList);
    ///   
    ///   Note: It's important to use sorted list
    /// </remarks>
    public sealed class ListSynchronizer {

        /// <summary>
        /// Synchronizes the sorted list.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sourceList">The source list.</param>
        /// <param name="destinationList">The destination list.</param>
        /// <param name="comparer">The comparer.</param>
        /// <param name="onlyOnSource">The only on source.</param>
        /// <param name="onlyOnDestination">The only on destination.</param>
        public static void SynchronizeSortedList<T> (IEnumerable<T> sourceList, IEnumerable<T> destinationList, IComparer<T> comparer, List<T> onlyOnSource, List<T> onlyOnDestination) {
            EnumerableIterator<T> sourceIterator = new EnumerableIterator<T> (sourceList);
            EnumerableIterator<T> destinationIterator = new EnumerableIterator<T> (destinationList);

            while (sourceIterator.HasCurrent && destinationIterator.HasCurrent) {
                while (sourceIterator.HasCurrent && (comparer.Compare (sourceIterator.Current, destinationIterator.Current) < 0)) {
                    onlyOnSource.Add (sourceIterator.Current);
                    sourceIterator.MoveNext ();
                }

                while (sourceIterator.HasCurrent && destinationIterator.HasCurrent && (comparer.Compare (sourceIterator.Current, destinationIterator.Current) > 0)) {
                    onlyOnDestination.Add (destinationIterator.Current);
                    destinationIterator.MoveNext ();
                }

                while (sourceIterator.HasCurrent && destinationIterator.HasCurrent && (comparer.Compare (sourceIterator.Current, destinationIterator.Current) == 0)) {
                    sourceIterator.MoveNext ();
                    destinationIterator.MoveNext ();
                }
            }

            while (sourceIterator.HasCurrent) {
                onlyOnSource.Add (sourceIterator.Current);
                sourceIterator.MoveNext ();
            }

            while (destinationIterator.HasCurrent) {
                onlyOnDestination.Add (destinationIterator.Current);
                destinationIterator.MoveNext ();
            }
        }
    }

    internal class EnumerableIterator<T> {
        private readonly IEnumerator<T> _enumerator;

        public EnumerableIterator (IEnumerable<T> enumerable) {
            _enumerator = enumerable.GetEnumerator ();
            MoveNext ();
        }

        public bool HasCurrent {
            get;
            private set;
        }

        public T Current {
            get {
                return _enumerator.Current;
            }
        }

        public void MoveNext () {
            HasCurrent = _enumerator.MoveNext ();
        }
    }

}